import React, { useEffect } from 'react';
import {Provider} from 'mobx-react';
import {store} from './store';
import RootRoute from './route/index';

export default function (): JSX.Element {
 
	useEffect(() => {
		const handler = event => {
		  const message = event.data; // 来自插件的数据
		  if (message.command === 'update') {
		    console.log('Received:', typeof message.content ,message.content);
			console.log('parse..', JSON.parse(message.content))
		    // 更新 Webview 内容
		     store.updateSchema(JSON.parse(message.content));
			 store.setPreview(false)
		  }
		}
		window.addEventListener('message',handler );
		document.addEventListener('keydown', function(e) {
		  // 检查 Ctrl 键或 Cmd 键（Mac）和 S 键
		  if ((e.ctrlKey || e.metaKey) && (e.key === 's' || e.keyCode === 83)) {
		    e.preventDefault();
		    handleSave();
		  }
		});
		
		function handleSave() {
		  console.log('Save triggered');
		  // 实现你的保存逻辑
		  // 例如：保存表单数据、保存编辑器内容等
		}
		return () => {
			window.removeEventListener("message", handler)
		}
	},[])
  return (
    <Provider store={store}>
      <RootRoute store={store} />
    </Provider>
  );
}
